package hackathon.light.up;

import java.util.List;

import com.e3roid.E3Scene;
import com.e3roid.drawable.Drawable;
import com.e3roid.drawable.Layer;
import com.e3roid.drawable.Shape;
import com.e3roid.event.ShapeEventListener;

import android.graphics.drawable.GradientDrawable.Orientation;
import android.util.Log;
import android.view.MotionEvent;

public class DoubleMirror extends Element {

	private int visited;

	public DoubleMirror(int row, int col, LightDirection dir) {
		super(row, col, dir);

		reset();
		
	}

	@Override
	public void reset() {
		super.allowedDirections.clear();
		super.allowedDirections
		.add(LightDirection.values()[(orientation.ordinal() + 3) % 8]);
			super.allowedDirections
		.add(LightDirection.values()[(orientation.ordinal() + 5) % 8]);

			super.allowedDirections
		.add(LightDirection.values()[(orientation.ordinal() + 1) % 8]);
		super.allowedDirections
		.add(LightDirection.values()[(orientation.ordinal() + 7) % 8]);
		super.lightSources.clear();
		super.lightSources.add(new LightSource(row, col, LightDirection.SE, 2));
		super.lightSources.add(new LightSource(row, col, LightDirection.SE, 2));
		super.lightSources.get(0).flagOn = false;
		super.lightSources.get(1).flagOn = false;
		super.flagOnScene = true;
		visited = 0;
	}

	@Override
	public void update(LightSource lightSource) {
		// TODO Auto-generated method stub
		if (flagOnScene) {
			if (visited<2) {

				for (int i = 0; i < allowedDirections.size(); i++) {

					Log.d("MIRROR", allowedDirections.get(i) + " "
							+ lightSource.direction);

					if (allowedDirections.get(i) == lightSource.direction) {

						visited ++;
						for (int j = 0; j < lightSources.size(); j++) {
							if (!lightSources.get(j).flagOn) {
								lightSources.get(j).flagOn = true;
								lightSources.get(j).color_id = lightSource.color_id;
								lightSources.get(j).direction = LightDirection
										.values()[(super.orientation.ordinal() + 1) % 8];
								if (i == 1) {
									lightSources.get(j).direction = LightDirection
											.values()[(super.orientation
											.ordinal() + 7) % 8];
								} else 
									if (i == 2) {
										lightSources.get(j).direction = LightDirection
												.values()[(super.orientation
												.ordinal() + 3) % 8];
									} else if (i == 3) {
										lightSources.get(j).direction = LightDirection
												.values()[(super.orientation
												.ordinal() + 5) % 8];
									}

								int next_col = col
										+ MapDirection.getNextCol(lightSources
												.get(j).direction);
								int next_row = row
										+ MapDirection.getNextRow(lightSources
												.get(j).direction);
								while (Grid.elementMatrix[next_row + 1][next_col + 1] == null) {
									next_col += MapDirection
											.getNextCol(lightSources.get(j).direction);
									next_row += MapDirection
											.getNextRow(lightSources.get(j).direction);
								}
								lightSources.get(j).drawLight(next_row,
										next_col);
								Grid.elementMatrix[next_row + 1][next_col + 1]
										.update(lightSources.get(j));
								return;
							}
						}
					}
				}
			}
		}
	}

}
